package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.*;
import com.cyzy.pojo.*;
import com.cyzy.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public  class SecKillServiceImpl extends ServiceImpl<SecKillMapper, Seckill> implements SecKillService {
    @Autowired
    private SecKillMapper secKillMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    // 安全获取Long值的辅助方法
    private Long getLongValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        if (value instanceof Integer) {
            return ((Integer) value).longValue();
        }
        if (value instanceof String) {
            try {
                return Long.parseLong((String) value);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("无法将字符串转换为Long: " + value, e);
            }
        }
        throw new IllegalArgumentException("不支持的类型: " + value.getClass() + "，键: " + key);
    }

    // 安全获取Double值的辅助方法
    private Double getDoubleValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Double) {
            return (Double) value;
        }
        if (value instanceof Integer) {
            return ((Integer) value).doubleValue();
        }
        if (value instanceof String) {
            try {
                return Double.parseDouble((String) value);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("无法将字符串转换为Double: " + value, e);
            }
        }
        throw new IllegalArgumentException("不支持的类型: " + value.getClass() + "，键: " + key);
    }

    // 安全获取String值的辅助方法
    private String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }

    @Override
    public List<Seckill> getAllSecKill() {
        QueryWrapper<Seckill> wrapper = new QueryWrapper<>();
        wrapper.ne("status","待审核");
        return secKillMapper.selectList(wrapper);
    }

    @Override
    public Seckill queryDetailBySeckillId(Long seckillId) {
        return secKillMapper.selectById(seckillId);
    }

    @Override
    public Course queryCourseById(Long courseId) {
        System.out.println("courseId的值为"+courseId);

        return courseMapper.selectById(courseId);
    }

    @Override
    public Boolean queryStuAuthen(Long studentId) {
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            return false;
        }

        String idCard = student.getIdCard();
        if (idCard == null || idCard.trim().isEmpty()) {
            return false;
        }
        String regex = "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|X|x)$";
        return idCard.matches(regex);

    }

    @Override
    public Long insertOrderBySecKill(Map<String, Object> map) {

        CourseOrder order = new CourseOrder();
        order.setOrderAmount(getDoubleValue(map, "orderAmount"));
        order.setStudentId(getLongValue(map, "studentId"));
        order.setSeckillId(getLongValue(map, "seckillId"));
        order.setCourseId(getLongValue(map, "courseId"));
        order.setActivateCode(getStringValue(map, "activateCode"));
        order.setCreateTime((Timestamp) map.get("createTime"));
        courseOrderMapper.insert(order);

        return order.getOrderId();
    }

    @Override
    public int updateSeckillStatus(Long seckillId, String status) {
        LambdaUpdateWrapper<Seckill> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Seckill::getSeckillId,seckillId);
        wrapper.set(Seckill::getStatus,status);
        return secKillMapper.update(wrapper);

    }

    @Override
    public int insertStudentCourse(Map<String, Object> map) {
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setStudentId(getLongValue(map, "studentId"));
        studentCourse.setCourseId(getLongValue(map, "courseId"));

        return studentCourseMapper.insert(studentCourse);
    }

    @Override
    public boolean queryUserLimit(Long studentId, Long seckillId, Long limitCount) {
        QueryWrapper<CourseOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id",studentId);
        wrapper.eq("seckill_id",seckillId);
        Long count = courseOrderMapper.selectCount(wrapper);
        System.out.println("用户购买的数量为："+count);
        if (count>=limitCount){
            return false;
        }else {
            return true;
        }

    }


}
